﻿using System;
using System.Linq.Expressions;
using nRoute.Components;
using nRoute.Utilities;
using System.ComponentModel;
using System.Windows;
#if (WINRT)
using Windows.UI.Xaml.Data;
using Windows.ApplicationModel;
using Windows.UI.Xaml;
#endif

namespace nRoute.ViewModels
{
    public abstract class ViewModelBase
		 : ISupportView, INotifyPropertyChanged
    {

#region ISupportView related

        void ISupportView.Loaded()
        {
            OnLoaded();
        }

        void ISupportView.Unloaded()
        {
            OnUnloaded();
        }

#endregion

#region Overridable

        protected virtual void OnLoaded() { }
        
        protected virtual void OnUnloaded() { }
        
#endregion

#region Helper

        protected bool IsInDesignMode()
        {
            if ((Application.Current != null) && (Application.Current.GetType() != typeof(Application)))
            {
#if (SILVERLIGHT)
                return DesignerProperties.IsInDesignTool;
#elif (!WINRT)
                // see http://stackoverflow.com/questions/4994696/wpf-getisindesignmode-from-inside-a-converter
                return DesignerProperties.GetIsInDesignMode(new DependencyObject());
#elif (WINRT)
                return DesignMode.DesignModeEnabled;
#endif
            }
            return true;
        }

#endregion

#region INotifyPropertyChanged related

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            Guard.ArgumentNotNullOrWhiteSpace("propertyName", propertyName);
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        protected void NotifyPropertyChanged<T>(Expression<Func<T>> propertySelector)
        {
            Guard.ArgumentNotNull(propertySelector, "propertySelector");
            PropertyChanged.Notify<T>(propertySelector);
        }

#endregion

    }
}